package com.cqust.icc.sqlite;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cqust.icc.sqlite.db.User;
import com.cqust.icc.sqlite.db.baseDbActivity;

import java.util.ArrayList;

public class MainActivity extends baseDbActivity {


    private ListView list_view;
    private EditText input_name;
    private EditText input_password;
    private Toast toast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text_view = findViewById(R.id.text_view);

        list_view = findViewById(R.id.list_view);
        input_name = findViewById(R.id.edit_name);
        input_password = findViewById(R.id.edit_password);

        String username = getIntent().getStringExtra("username");
        if (TextUtils.isEmpty(username)) {
            finish();
        }
        text_view.setText(String.format("欢迎您！%s", username));

        UpdateListView();
    }

    public void ShowToast(String text) {
        if (toast == null) {
            toast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    /*
    * 更新ListView 数据
    * */
    private void UpdateListView() {
        ArrayList<User> data = dbService.getPersonList();

        MyAdapter adapter = new MyAdapter(data, MainActivity.this);
        adapter.setMyClick(new MyClick() {
            @Override
            public void deleteClick(User user) {
                dbService.delete(user.getName());
                UpdateListView();
                ShowToast("删除成功~");
            }

            @Override
            public void addClick(User user) {
                ContentValues value = new ContentValues();
                value.put("username", user.getName());
                value.put("password", user.getPassword());
                value.put("age", user.getAge() + 1);
                dbService.update(value);
                UpdateListView();
                ShowToast("更新成功~");
            }

            @Override
            public void subClick(User user) {
                ContentValues value = new ContentValues();
                value.put("username", user.getName());
                value.put("password", user.getPassword());
                value.put("age", user.getAge() - 1);
                if (user.getAge() - 1 < 0) {
                    value.put("age", 0);
                }

                dbService.update(value);
                UpdateListView();
                ShowToast("更新成功~");
            }


        });
        list_view.setAdapter(adapter);
    }


    public void addClick(View view) {
        String name = input_name.getText().toString();
        String pwd = input_password.getText().toString();


        if (TextUtils.isEmpty(name)) {
            ShowToast("请输入账号~");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ShowToast("请输入密码~");
            return;
        }
        ContentValues value = new ContentValues();
        value.put("username", name);
        value.put("password", pwd);
        try {
            dbService.insertPerson(value);
            ShowToast("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            ShowToast("账号已经存在");
        }
        UpdateListView();
    }
}